
package org.springblade.modules.videoanswer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.modules.videoanswer.entity.Group;
import org.springblade.modules.videoanswer.vo.GroupVO;
import org.springblade.modules.videoanswer.mapper.GroupMapper;
import org.springblade.modules.videoanswer.service.IGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * 群组管理 服务实现类
 *
 * @author jiangyandong
 * @since 2021-03-22
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {

	@Override
	public IPage<GroupVO> selectGroupPage(IPage<GroupVO> page, GroupVO group) {
		return page.setRecords(baseMapper.selectGroupPage(page, group));
	}

	@Override
	public List<Group> noPagelist(List<Group> groups){
		BladeUser loginUser = SecureUtil.getUser();
		String roleId = loginUser.getRoleId();
		List<Group> groupList;
		List<Integer> groupIds = new ArrayList<>();
		for(Group group : groups){
			groupIds.add(group.getId());
		}
		if("1".equals(roleId)){
			//超级管理员查询全部
			if(groupIds.size() > 0){
				groupList = this.list(Wrappers.<Group>query().lambda().notIn(Group::getId,groupIds));
			} else {
				groupList = this.list();
			}
		} else {
			//普通管理员
			Integer userid = loginUser.getUserId();
			if(groupIds.size() > 0){
				groupList = this.list(Wrappers.<Group>query().lambda().eq(Group::getUserId, userid).notIn(Group::getId,groupIds));
			} else {
				groupList = this.list(Wrappers.<Group>query().lambda().eq(Group::getUserId, userid));
			}
		}
		return groupList;
	}

}
